package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.Finished
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.fmt_comments.view.*
import kotlinx.android.synthetic.main.item_comment_finished.*
import kotlinx.android.synthetic.main.item_comment_finished.view.*
import kotlinx.android.synthetic.main.item_comment_unfinished.view.*
import kotlinx.android.synthetic.main.item_comment_unfinished.view.tv_paperSubname
import kotlinx.android.synthetic.main.item_comment_unfinished.view.tv_questiontype
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern
import kotlinx.android.synthetic.main.item_comment_finished.view.tv_week as tv_week1
import kotlinx.android.synthetic.main.item_comment_unfinished.view.tv_answertime as tv_answertime1
import kotlinx.android.synthetic.main.item_comment_unfinished.view.view as view1


/**
 * @author : Admin
 * @date   : 2019/7/17 20:03
 * @desc   :
 */
class QuestionListAdapter(data: MutableList<Finished>?) :
        BaseMultiItemQuickAdapter<Finished, BaseViewHolder>(data) {
    init {
        addItemType(0, R.layout.item_comment_unfinished) //未辅导
        addItemType(1, R.layout.item_comment_finished) //已辅导
    }

    @SuppressLint("SimpleDateFormat", "SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: Finished?) {
        helper.addOnClickListener(R.id.textView15)
        helper.addOnClickListener(R.id.textView16)
        with(helper.itemView) {
            if (isOutWeek(item!!.UpdateTime) && item.isShow) {
                view.visibility = View.VISIBLE
                tv_week.visibility = View.VISIBLE
            } else {
                view.visibility = View.GONE
                tv_week.visibility = View.GONE
            }
            tv_questiontype.text = if (item.GenreName.isBlank()) item.TypeName else item.GenreName
            if (item.PaperName.isBlank()) {
                tv_paperSubname.text = "(${item.SetName})"
            } else {
                if (item.PaperName.lastIndexOf("（") != -1) {
                    tv_paperSubname.text = "(${
                        item.PaperName.substring(0, item.PaperName.lastIndexOf("（"))
                    }-${item.SetName})"
                } else {
                    tv_paperSubname.text = "(${item.PaperName})"
                }
            }
            when (helper.itemViewType) {
                0 -> {
                    tv_percent_lost_str.text = item.ScoreRate.times(100)
                        .let { "${BigDecimal(it).setScale(0, BigDecimal.ROUND_HALF_UP)}" + "%" }
                    val duration = item.Duration
                    setAnswerTime(context, duration, tv_answertime)
                }
                1 -> {
                    tv_read_numstr.text = item.ReadCount.toString()
                    tv_finished_numstr.text = item.PassedCount.toString()
                    tv_unfinished_numstr.text = item.FailedCount.toString()
                }
            }
        }
    }

    companion object {
        fun setAnswerTime(context: Context, duration: String, tv: TextView) {
            val chinese = "[\\u4e00-\\u9fa5]"
            val pat = Pattern.compile(chinese)
            if (duration == "--") {
                tv.text = "平均作答时长：--"
            } else {
                val mat = pat.matcher(duration)
                val durationStr = mat.replaceAll("\\|")
                val time = durationStr.split("|").dropLastWhile { it.isEmpty() }
                var hour = ""
                var minute = ""
                var second = ""
                var hourStr = ""
                var minuteStr = ""
                var secondStr = ""
                if (time.size == 3) {
                    hourStr = "小时"
                    minuteStr = "分钟"
                    secondStr = "秒"
                    hour = time[0]
                    minute = time[1]
                    second = time[2]
                } else if (time.size == 2) {
                    if (duration.contains("时")) {
                        hourStr = "小时"
                        minuteStr = "分钟"
                        secondStr = ""
                        hour = time[0]
                        minute = time[1]
                        second = ""
                    } else if (duration.contains("分")) {
                        hourStr = ""
                        minuteStr = "分钟"
                        secondStr = "秒"
                        hour = ""
                        minute = time[0]
                        second = time[1]
                    }
                } else if (time.size == 1) {
                    if (duration.contains("时")) {
                        hourStr = "小时"
                        minuteStr = ""
                        secondStr = ""
                        hour = time[0]
                        minute = ""
                        second = ""
                    } else if (duration.contains("分")) {
                        hourStr = ""
                        minuteStr = "分钟"
                        secondStr = ""
                        hour = ""
                        minute = time[0]
                        second = ""
                    } else if (duration.contains("秒")) {
                        hourStr = ""
                        minuteStr = ""
                        secondStr = "秒"
                        hour = ""
                        minute = ""
                        second = time[0]
                    }
                }
                tv.text = HtmlUtils.setText(
                    context.getString(R.string.avgtime, hour, hourStr, minute, minuteStr, second,
                        secondStr))
            }
        }

        fun isOutWeek(time: String): Boolean {
            val updateCalendar = Calendar.getInstance().apply {
                val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
                setTime(sdf.parse(time)!!)
            }
            val calendarWeek = updateCalendar.apply {
                add(Calendar.DATE, 7)
            }
            val calendar = Calendar.getInstance()
            return calendarWeek.before(calendar)
        }
    }
}